<?php

/**
 * preceptor actions.
 *
 * @package    ecampus
 * @subpackage preceptor
 * @author     Grupo 608
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class preceptorActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $this->redirect('usuario/index');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    $this->preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($this->preceptor);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PreceptorForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PreceptorForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
     $this->id_usuario = $request->getParameter('id_usuario');
    $this->preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario'))), sprintf('Object preceptor does not exist (%s).', $request->getParameter('id_usuario')));
//    $this->form = new PreceptorForm($preceptor);
    $this->form = new EditarPreceptorForm($preceptor);
   
   
  }

  public function executeUpdate(sfWebRequest $request)
  {

     $this->preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario'))), sprintf('Object preceptor does not exist (%s).', $request->getParameter('id_usuario')));
  //  $this->form = new PreceptorForm($preceptor);

    $this->form = new EditarPreceptorForm($preceptor);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
//    $request->checkCSRFProtection();

    $this->forward404Unless($preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario'))), sprintf('Object preceptor does not exist (%s).', $request->getParameter('id_usuario')));
    $preceptor->delete();

    $this->redirect('preceptor/index');
  }

  public function executeDatos(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');

    $this->preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario'))), sprintf('Object preceptor does not exist (%s).', $request->getParameter('id_usuario')));
    $this->datos = new PreceptorForm($preceptor);
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $preceptor = $form->save();

      $this->redirect('preceptor/index?id_usuario='.$preceptor->getIdUsuario());
    }
  }
  public function executeMensajes(sfWebRequest $request)
   {
       $this->id_usuario = $request->getParameter('id_usuario');
       $this->preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario')));
       $this->mensajes = MensajeTable::getMensajesDeUsuario($request->getParameter('id_usuario'));
   }
  public function executeAlertas(sfWebRequest $request)
  {
      $this->borrar = $request->getParameter('borrar');
      
      $this->marcar_leido = $request->getParameter('marcar_leido');
      $this->tipoUsuario = 'preceptor';

      $this->id_usuario = $request->getParameter('id_usuario');

      $this->preceptor = Doctrine::getTable('Preceptor')->find(array($request->getParameter('id_usuario')));
      $this->usuario = $this->preceptor;
      $this->alertas = Doctrine_Core::getTable('Usuario')->getAlertas($request->getParameter('id_usuario'));
      $this->alertasNoLeidas = Doctrine_Core::getTable('Usuario')->getAlertasNoLeidas($request->getParameter('id_usuario'));
   }
  public function executeAsistencia(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');
  }
  public function executeBuscar(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');
  }
  public function executeListado(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');
    
      $this->anio = $request->getParameter('anio');
      $this->alumnos = Doctrine_Core::getTable('alumno')->obteneralumnosporanio($request->getParameter('anio'));
      $this ->g_fecha= $request->getParameter('fecha');
      $this->presente = array();
      $this->obser = array();
      foreach ($this -> alumnos as $alumno):
         $this -> presente[$alumno ->  getIdUsuario()]=$request->getParameter('presente_' . strval($alumno ->  getIdUsuario()));
         $this -> obser[$alumno ->  getIdUsuario()]=$request->getParameter('obs_' . strval($alumno ->  getIdUsuario()));
      endforeach;

      $this->Consultar = $request->getParameter('Consultar');

      }
  public function executeAlumnos(sfWebRequest $request)
  {
      $this->id_usuario = $request->getParameter('id_usuario');

      $this->anio = $request->getParameter('anio');
      $this->alumnos = Doctrine_Core::getTable('alumno')->obteneralumnosporanio($request->getParameter('anio'));
      foreach ($this -> alumnos as $alumno):
         $this -> presente[$alumno ->  getIdUsuario()]=$request->getParameter('presente_' . strval($alumno ->  getIdUsuario()));
      endforeach;
   }
   public function executeAlumnosLibres(sfWebRequest $request)
   {
       $this->id_usuario = $request->getParameter('id_usuario');
       
       $this->borrar = $request->getParameter('borrar');
       
       $this->alumnosLibres = Doctrine_Query::create()
//                ->select('a.id_usuario as id,a.nombre as nombre,a.apellido as apellido,count(p.presente) as total')
//                ->from('alumno a')
//                        ->leftJoin('asistencia p WITH a.id_usuario=p.id_alumno')
//                ->where('p.presente=?',0)
//                        ->andWhere('p.activo = ?',1)
//                ->groupBy('a.id_usuario')
//                ->having('count(p.presente) >= ?', 2 /* sfConfig::get('limite_faltas') */ ) //el 2º parametro es el limite de faltas permitidos
////                ->fetchArray()
//               ->execute()
//                ;
               ->select('a.id_alumno id,count(a.presente) total')
               ->from('asistencia a')
               ->where('a.activo = ? and a.presente = ?',array(1,0) )
               ->groupBy('a.id_alumno')
               ->having('count(a.presente) >= ?',15)
//               ->execute()
               ->fetchArray()
               ;

   }
   
   public function executeConsultarAsistencia(sfWebRequest $request)
  {
      $this->anio = $request->getParameter('anio');
      $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
      $this->asistencias = Doctrine::getTable('Asistencia')
              ->createQuery('a')
              ->where('a.id_alumno = ? and a.activo = ? and a.presente = ?',array($request->getParameter('id_usuario'),1,0))
              ->execute();

     }



}
